package com.zpc.myvideoserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.zpc.myvideoserver.common.util.ResponseJson;
import com.zpc.myvideoserver.entity.Collect;
import com.zpc.myvideoserver.entity.Vo.CollectVo;
import com.zpc.myvideoserver.service.CollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 视频收藏表 前端控制器
 * </p>
 *
 * @author zpc
 * @since 2023/11/03
 */
@RestController
@RequestMapping("/collect")
@Api(tags = "收藏视频Api")
public class CollectController {

       @Autowired
       CollectService collectService;
       @GetMapping("/getUserColletct")
       @ApiOperation("获取用户收藏")
       public ResponseJson<List<Collect>> getUserColletct(@RequestParam(value = "userId") Integer userId){
             QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
             List<Collect> collects = collectService.getBaseMapper().selectList(queryWrapper.eq("user_id",userId));
             if(!collects.isEmpty()){
                 return ResponseJson.SuccessResponse("查询成功!",collects);
             }else {
                 return new ResponseJson<List<Collect>>("不存在",404,null);
             }

       }
    @PostMapping("/insert")
    @ApiOperation("收藏video")
    public ResponseJson<Collect> insert(
            @RequestBody Collect collect
            ){
           QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        Collect hascollect = collectService.getOne(collectQueryWrapper.eq("video_id",collect.getVideoId()).eq("user_id",collect.getUserId()));
        if(hascollect==null){
            boolean result = collectService.save(collect);
            if(result){
                return ResponseJson.SuccessResponse("收藏成功!",collect);
            }else {
                return ResponseJson.SuccessResponse("收藏失败!",null);
            }
        }else {
            return new ResponseJson<>("重复收藏!",403,null);
        }
    }

    @ApiOperation(value = "删除数据",notes = "通过collectId删除")
    @DeleteMapping("/deleteById")
    public ResponseJson<Collect> deleteCollect(@RequestParam("id") String Id){
           QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
           Collect collect =collectService.getById(Id);
           boolean res = collectService.remove(queryWrapper.eq("id",Id));
           if(res){
               return ResponseJson.SuccessResponse("删除成功!", collect);
           }else {
               return ResponseJson.FailResponse("删除失败！",null);
           }
    }
    @ApiOperation(value = "getByIds",notes = "通过videoId获取信息")
    @GetMapping("/getByIds")
    public ResponseJson<Collect> getByVideoId(@RequestParam("videoId") String videoId,@RequestParam("userId") String userId){
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        Collect collect =collectService.getOne(queryWrapper.eq("video_id",videoId).eq("user_id",userId));
        if(collect!=null){
            return ResponseJson.SuccessResponse("获取成功!", collect);
        }else {
            return new ResponseJson<Collect>("不存在！",404,null);
        }
    }
}
